package com.ruoyi.common.core.utils;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 作用：线程控制器
 * </p>
 *
 * @author Zane
 * @since 2025-10-25 16:09
 */
public class LatchUtils {

  private static final ThreadLocal<List<TaskInfo>> THREADLOCAL = ThreadLocal.withInitial(
      LinkedList::new);

  public static void submitTask(Executor executor, Runnable runnable) {
    THREADLOCAL.get().add(new TaskInfo(executor, runnable));
  }

  private static List<TaskInfo> popTask() {
    List<TaskInfo> taskInfos = THREADLOCAL.get();
    THREADLOCAL.remove();
    return taskInfos;
  }

  public static boolean runAndWait(long timeout, TimeUnit timeUnit) {
    List<TaskInfo> taskInfos = popTask();
    if (taskInfos.isEmpty()) {
      return true;
    }
    CountDownLatch latch = new CountDownLatch(taskInfos.size());
    for (TaskInfo taskInfo : taskInfos) {
      Executor executor = taskInfo.executor;
      Runnable runnable = taskInfo.runnable;
      executor.execute(() -> {
        try {
          runnable.run();
        } finally {
          latch.countDown();
        }
      });
    }
    boolean await = false;
    try {
      await = latch.await(timeout, timeUnit);
    } catch (Exception ignored) {
    }
    return await;
  }

  private static final class TaskInfo {

    private final Executor executor;
    private final Runnable runnable;

    public TaskInfo(Executor executor, Runnable runnable) {
      this.executor = executor;
      this.runnable = runnable;
    }
  }
}

